<?php

namespace app\system\controller;
use think\Db;


class Index extends Base
{
    /*
     * @Note 后台首页
     * @Date 2021/06/21
     * @Time 10:30
     */
    public function index()
    {
        $this->assign('name', web_config('name'));
        return $this->fetch();
    }


    // 欢迎页
    public function welcome()
    {

        //系统参数
        $data = [
            'server_name' => $_SERVER['SERVER_NAME'],
            'uname' => php_uname('s'),
            'server_software' => $_SERVER["SERVER_SOFTWARE"],
            'php_version' => PHP_VERSION,
            'php_sapi_name' => php_sapi_name(),
            'max_upload' => ini_get("file_uploads") ? ini_get("upload_max_filesize") : "Disabled",
            'max_ex_time' => ini_get("max_execution_time") . "s"
        ];        
        return $this->fetch('', [
            'data' => $data
        ]);
    }

    // 图标页
    public function icon() {
        return $this->fetch();
    }

    // 获取初始化数据
    public function getSystemInit()
    {
        $homeInfo = [
            'title' => '首页',
            'href' => 'welcome',
        ];
        $logoInfo = [
            'title' =>  web_config('name'),
            'image' => web_config('image'),
            'href' => 'index',
        ];
        $menuInfo = $this->getMenuList();
        $systemInit = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
        return json($systemInit);
    }

    // 获取菜单列表
    private function getMenuList()
    {
        $menuList = Db::name('system_menu')
            ->field('id,pid,title,icon,href,target')
            ->where('status', 1)
            ->order('sort', 'desc')
            ->select();
        $menuList = $this->buildMenuChild(0, $menuList);
        return $menuList;
    }
    //递归获取子菜单
    private function buildMenuChild($pid, $menuList)
    {
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v['pid']) {
                // 此处用户的权限判断
                if(session('aid') != 1){
                    if(!in_array($v['id'], $this->adminRules)){
                        unset($v);
                    }else {
                        $node = $v;
                        $child = $this->buildMenuChild($v['id'], $menuList);
                        if (!empty($child)) {
                            $node['child'] = $child;
                        }
                      
                        $treeList[] = $node;
                    }
                }else {
                    $node = $v;
                        $child = $this->buildMenuChild($v['id'], $menuList);
                        if (!empty($child)) {
                            $node['child'] = $child;
                        }
                      
                    $treeList[] = $node;
                }

            }
        }
        return $treeList;
    }
    public function clear(){
        if ($this->_deleteDir(RUNTIME_PATH)) {
            return ajaxArray(1,'清除缓存成功');
        } else {
            return ajaxArray(0,'清除缓存失败');
        }
    }

    private function _deleteDir($R){
        $handle = opendir($R);
        while (($item = readdir($handle)) !== false) {
            if ($item != '.' and $item != '..') {
                if (is_dir($R . '/' . $item)) {
                    $this->_deleteDir($R . '/' . $item);
                } else {
                    if (!unlink($R . '/' . $item))
                        die('error!');
                }
            }
        }
        closedir($handle);
        return rmdir($R);
    }
    // 修改个人资料
    public function user_setting() {
        $system_id = session('aid');
        if(request()->isPost()) {
           $data = input('post.');
           $res = model('admin')->where('id',$system_id)->update($data);
           system_log(['menu'=> '个人信息', 'descr'=> '修改登录账号名称', 'create_time' => time()]);
           if($res) return ajaxArray(1,'修改成功');
           return ajaxArray(0,'修改失败'); 
        }
        $admin = model('admin')->where('id',$system_id)->find();
        $this->assign('admin',$admin);
        return $this->fetch();
    }

    // 修改密码
    public function user_password() {
        if(request()->isPost()) {
            $data = input('post.');
            $system_id = session('aid');
            $admin = model('admin')->where('id',$system_id)->find();
            if(md5($data['old_password']) != $admin['password']) return ajaxArray(0,'输入旧密码错误');
            if($data['new_password'] != $data['again_password']) return ajaxArray(0,'输入的两次密码不一致');
            $password = md5($data['new_password']);
            if($password == $admin['password']) return ajaxArray(0,'新密码不可与原密码相同');
            $res = model('admin')->where('id',$admin['id'])->update(['password' => $password]);
            system_log(['menu'=> '修改密码', 'descr'=> '修改登录账号密码', 'create_time' => time() ]);
            if($res) return ajaxArray(1,'修改成功');
            return ajaxArray(0,'修改失败'); 
        }
        return $this->fetch();
    }
}
